<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <!-- 引入Bootstrap CSS框架 -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .restaurant-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            gap: 20px;
        }
        .restaurant {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            flex: 0 0 calc(33.333% - 20px);
            margin-bottom: 20px;
        }
        .restaurant img {
            width: 100%;
            display: block; /* Remove extra space below the image */
        }
        .info {
            padding: 15px;
        }
        .restaurant-name, .restaurant_phone, .restaurant_address {
            margin-bottom: 5px;
            font-size: 16px;
            color: #555;
        }
        .restaurant button {
            margin-top: 15px;
        }
        .btn-link {
            display: block;
            padding: 10px;
            text-align: center;
            color: #fff;
            background-color: #007BFF;
            border-radius: 5px;
            text-decoration: none;
            transition: background-color 0.3s ease;
        }
        .btn-link:hover {
            background-color: #0056b3;
        }
        .side {
            margin-top: 20px;
            padding: 20px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }
        form {
            max-width: 300px;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
<h1>餐馆列表</h1>
<div class="restaurant-list">

<%
javax.sql.rowset.WebRowSet rs = null;
try {
    rs = com.myweb.DBTools.getRS("SELECT * FROM restaurants");
    while (rs.next()) {
        String restaurantId = rs.getString("restaurant_id");
        String restaurantPic = rs.getString("restaurant_pic");
        String restaurantName = rs.getString("restaurant_name");
        String restaurantPhone = rs.getString("restaurant_phone");
        String restaurantAddress = rs.getString("restaurant_address");
        
        // 对URL参数进行HTML转义，防止XSS攻击
        restaurantId = java.net.URLEncoder.encode(restaurantId, "UTF-8");
%>
<div class="restaurant">
    <img src="img/<%= restaurantPic %>" alt="restaurant Image" width="304" height="228"/>
    <div class="info">
        <div class="restaurant_id"><%= restaurantId %></div>
        <div class="name">
            <i class="fa fa-map-marker"></i>
            <span class="restaurant_name"><%= restaurantName %></span>
        </div>
        <div class="phone">
            <i class="fa fa-phone"></i>
            <span class="restaurant_phone"><%= restaurantPhone %></span>
        </div>
        <div class="address">
            <i class="fa fa-map-marker"></i>
            <span class="restaurant_address"><%= restaurantAddress %></span>
        </div>
        <!-- URL编码restaurant_id，确保URL安全 -->
        <a href="menu.jsp?restaurant_id=<%= restaurantId %>" class="btn-link">点餐</a>
    </div>
</div>
<%
    }
} catch (Exception e) {
    // 记录日志或显示错误信息
    e.printStackTrace();
    out.println("无法加载餐馆列表，请稍后再试或联系管理员。");
} finally {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            // 记录日志
            e.printStackTrace();
        }
    }
}
%>

<div class="side">  
    <form action="select_table" method="post">  
        <h2>自主选餐馆与座位~</h2>  
        <label for="restaurant_id">餐馆号:</label><br>  
        <input type="text" id="restaurant_id" name="restaurant_id" required><br>  
        <label for="select_table">座位号:</label><br>  
        <input type="text" id="select_table" name="select_table" required><br>  
        <button type="submit">选定餐馆与座位</button>  
    </form>  
</div>  

</div>
</body>
</html>